package com.zhangkunhao.notes.service;

import com.zhangkunhao.notes.dao.PermissionBlacklistDao;
import com.zhangkunhao.notes.entity.Do.*;

import com.zhangkunhao.notes.entity.bo.Message;

import com.zhangkunhao.notes.entity.bo.UserPermissionPage;
import com.zhangkunhao.notes.service.impl.PermissionBlacklistService;
import com.zhangkunhao.notes.utils.IfValidInputUtil;


import java.sql.SQLException;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 不同权限拉黑列表接口的实现类
 *
 * @author 20642
 */
public class PermissionBlacklistServiceImpl implements PermissionBlacklistService {

    @Override
    public List<PermissionBlacklist> selectAll() {
        return PermissionBlacklistDao.getInstance().findAll();
    }

    @Override
    public void insert(PermissionBlacklist permissionBlacklist) throws Exception {
        PermissionBlacklistDao.getInstance().insert(permissionBlacklist);
    }

    @Override
    public int delete(int id) throws SQLException {
        return PermissionBlacklistDao.getInstance().delete(id);
    }

    @Override
    public int update(PermissionBlacklist permissionBlacklist, String... params) throws Exception {
        return PermissionBlacklistDao.getInstance().update(permissionBlacklist);
    }


    public UserPermissionPage listUserPrivileges() {
        //调用service层查询
        List<User> users = new UserServiceImpl().selectAll();
        List<Permission> permissions = new PermissionServiceImpl().selectAll();
        List<PermissionBlacklist> permissionBlacklists = new PermissionBlacklistServiceImpl().selectAll();
        //返回数据
        return new UserPermissionPage(users, permissions, permissionBlacklists, users.size());
    }

    public Message<PermissionBlacklist> setUserPrivilege(PermissionBlacklist permissionBlacklist) throws Exception {
        //初始化
        Message<PermissionBlacklist> permissionBlacklistMessage = new Message<>(null, "封禁用户权限失败", 0);
        //判断
        if (permissionBlacklist == null) {
            permissionBlacklistMessage.setResult("该用户权限为空，封禁失败");
            return permissionBlacklistMessage;
        }
        if (!IfValidInputUtil.ifValidValue(permissionBlacklist.getBanPermissionName())) {
            permissionBlacklistMessage.setResult("没有选择封禁的权限名称，封禁失败");
            return permissionBlacklistMessage;
        }
        if (permissionBlacklist.getOpenDate() == null) {
            permissionBlacklistMessage.setResult("没有设置封禁时间，封禁失败");
            return permissionBlacklistMessage;
        }
        System.out.println("permissionBlacklist.getOpenDate():" + permissionBlacklist.getOpenDate());

        //获取被封禁的权限的id
        PermissionServiceImpl permissionService = new PermissionServiceImpl();
        Permission permission1 = permissionService.selectAll().stream().filter(permission -> permission.getPermissionName()
                .equals(permissionBlacklist.getBanPermissionName())).collect(Collectors.toList()).get(0);

        //修改
        permissionBlacklist.setBanPermissionId(permission1.getId());

        //校验是否已经封禁过
        boolean empty = this.selectAll().stream().filter(permissionBlacklist1 -> permissionBlacklist1.getBanPermissionId().equals(permissionBlacklist.getBanPermissionId()
        ) && permissionBlacklist1.getBanUserId().equals(permissionBlacklist.getBanUserId())).collect(Collectors.toList()).isEmpty();

        if (!empty) {
            permissionBlacklistMessage.setResult("已经设置过封禁时间，封禁失败，请先取消封禁再重新设置");
            return permissionBlacklistMessage;
        }

        //调用dao
        this.insert(permissionBlacklist);


        //提供成功与否的判断
        boolean anyMatch = this.selectAll()
                .stream()
                .anyMatch(permissionBlacklist1 -> permissionBlacklist1.getBanPermissionId().equals(permissionBlacklist.getBanPermissionId())
                        && permissionBlacklist1.getBanUserId().equals(permissionBlacklist.getBanUserId()));

        //成功
        if (anyMatch) {
            permissionBlacklistMessage.setResult("封禁该权限成功");
            permissionBlacklistMessage.setStatus(1);
        }
        //返回数据
        return permissionBlacklistMessage;
    }

    public Message<PermissionBlacklist> cancelSetUserPrivilege(int losePermissionId) throws Exception {
        //初始化
        Message<PermissionBlacklist> permissionBlacklistMessage = new Message<>(null, "取消封禁用户权限失败", 0);

        //调用dao
        int delete = this.delete(losePermissionId);

        //成功
        if (delete > 0) {
            permissionBlacklistMessage.setResult("取消封禁该权限成功");
            permissionBlacklistMessage.setStatus(1);
        }
        //返回数据
        return permissionBlacklistMessage;
    }

}
